#!/usr/bin/python

import os
import sys,getopt
import re
import JoDef
import JoStore
import xml.dom.minidom

class interview:
	"""Interview class, reads command xml and creates interview"""
	interview = None
	ivdom = None

	def __init__(self,command,opts):
		ivpath = os.getenv("JOIVPATH",JoDef.JOIVDIR)
		self.ivdom = xml.dom.minidom.parse(ivpath+"/"+command+".xml")
		self.insert_opts(opts)

	def insert_opts(self,opts):
		if not opts: return
		ivnode = self.ivdom.getElementsByTagName("interview")[0]
		for key in opts.get_store():
			if opts[key]:
				storenode = self.ivdom.createElement("storage")
				storenode.setAttribute("key",key)
				storenode.setAttribute("value",opts[key])
				ivnode.appendChild(storenode)

	def sub(self,pat,repl):
		if not pat:
			return
		if not repl:
			repl = ""
		self.interview = re.sub(pat,repl,self.interview)

	def get_xml(self):
		return self.interview

	def xmit(self):
		self.ivdom.writexml(sys.stdout)

	def _print(self):
		return self.interview
